package ljp.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ljp.entity.GoodsEntity;
import ljp.mapper.GoodsDao;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName GoodsService
 * @Description TODO
 * @Author LJP
 * @Date 2022/10/15 20:50
 */
@Service

public class GoodsService extends ServiceImpl<GoodsDao, GoodsEntity> {
    /**
    *
     * @Description: 获取数据库中所有信息
     * @return: 商品列表
     * @Author: LJP
     */
    public List<GoodsEntity> getAll(){
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();
        return list(queryWrapper);
    }

    /**
    *
     * @Description: 获取特定商品信息
     * @param id 商品的id
     * @return: entity.ljp.GoodsEntity
     * @Author: LJP
     */
    public GoodsEntity getGoodById(String id){
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(GoodsEntity::getId,id);
        return this.getOne(queryWrapper);
    }

    public GoodsEntity getGoodByName(String name){
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(GoodsEntity::getName,name);
        return this.getOne(queryWrapper);
    }

    /**
    *
     * @Description: 更新某个商品的信息
     * @return: java.lang.Boolean
     * @Author: LJP
     */
    public Boolean updateOne(String id,GoodsEntity good){
        UpdateWrapper<GoodsEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(GoodsEntity::getId,id);
        return this.update(good,updateWrapper);
    }

    /**
    *
     * @Description: 创建某一个商品
     * @param good 商品信息
     * @return:
     * @Author: LJP
     */
    public Boolean create(GoodsEntity good){
        String uuid = UUID.randomUUID().toString();
        good.setId(uuid);
        return this.save(good);
    }

    public Boolean deleteById(String id){
        return this.removeById(id);
    }
}
